package domain.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class Parser {

	public static String getParser(String filename) {
		StringBuilder result = new StringBuilder();
		
		Scanner sc = null;
		try {
			sc = new Scanner(new File(filename));
		} catch (FileNotFoundException e) {
			System.out.println("Erro ao abrir o arquivo: " + filename);
			System.exit(1);
		}
		
		
		while (sc.hasNextLine()) {
			//exemplo de regra: "title=245$a"
			String rule = sc.nextLine();
			
			String first = rule.split(Pattern.quote("="))[0];
			String second = rule.split(Pattern.quote("="))[1];
			
			for (String code : second.split(Pattern.quote(","))) {
				System.out.println("if (key.equals(\"" + code + "\")) {");
				System.out.println("\tresource.set" + first.substring(0, 1).toUpperCase() + first.substring(1) + "(value);");
				System.out.println("\treturn;");
				System.out.println("}");
			}
		}

		System.out.println("resource.setMarcCode( (resource.getMarcCode() + \"\\n\" + key + \"\\t\" + value).trim() );" );
		return result.toString();
	}
	
	public static void main(String[] args) {
		Parser.getParser("codigosMarc.txt");
	}
}
